package com.nazca.tvremote.service;

import java.util.HashSet;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;

import com.nazca.tvremote.service.IEventService;
import com.nazca.tvremote.service.IEventServiceCallback;
import com.nazca.tvremote.util.RLog;

public class Controller {

	private static final String TAG = "Controller";
	
//	private EventServiceProxy mEventServiceProxy = null;
	
	public static Controller sInstance;
	
	private Context mContext;
	
	private ServiceCallback mServiceCallback = new ServiceCallback();
	private HashSet<Result> mListeners = new HashSet<Result>();
	
	public synchronized static Controller getInstance(Context _context) {
		_context.startService(new Intent(_context, EventService.class));
        if (sInstance == null) {
            sInstance = new Controller(_context);
        }
        return sInstance;
    }
	
	protected Controller(Context _context) {
        mContext = _context;
//        mLegacyController = MessagingController.getInstance(mContext);
//        mLegacyController.addListener(mLegacyListener);
//        mEventServiceProxy = (EventServiceProxy) getEventService(_context, mServiceCallback);
//        mEventServiceProxy.bindService();
    }
	
//	public void closeController(){
//		mEventServiceProxy.unBindService();
//		sInstance = null;
//	}
	
	public void connectToServer(String dstAddress, int dstPort){
		IEventService iService = getEventService(mContext, mServiceCallback);
		try {
			iService.connectToServer(dstAddress, dstPort);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	public void disConnectToServer(){
		IEventService iService = getEventService(mContext, mServiceCallback);
		try {
			iService.disConnectToServer();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		mContext.stopService(new Intent(mContext, EventService.class));
	}
	
	public void println(String line){
		IEventService iService = getEventService(mContext, mServiceCallback);
		try {
			iService.println(line);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 回调给UI
	 * @author Administrator
	 *
	 */
	public interface Result{
		public void connectToServer(boolean successful);
		public void disConnectToServer(boolean successful);
	}
	
	public void addResultCallback(Result listener) {
        synchronized (mListeners) {
            mListeners.add(listener);
        }
    }
	
	public void removeResultCallback(Result listener) {
        synchronized (mListeners) {
            mListeners.remove(listener);
        }
    }
	
	
	private class ServiceCallback extends IEventServiceCallback.Stub{

		@Override
		public void connectToServer(boolean successful) throws RemoteException {
			RLog.d(TAG, "connectToServer()");
			RLog.d(TAG, "successful:"+successful);
			synchronized (mListeners) {
                for (Result listener : mListeners) {
                	listener.connectToServer(successful);
                }
            }
		}

		@Override
		public void disConnectToServer(boolean successful)
				throws RemoteException {
			RLog.d(TAG, "disConnectToServer()");
			RLog.d(TAG, "successful:"+successful);
			synchronized (mListeners) {
				RLog.d(TAG, "mListeners.size():" + mListeners.size());
                for (Result listener : mListeners) {
                	listener.disConnectToServer(successful);
                }
            }
		}
	}
	
	private static IEventService getEventService(Context context,
			IEventServiceCallback callback) {
		IEventService ret = null;
		ret = new EventServiceProxy(context, EventService.class, callback);
        return ret;
    }

}
